//
//  ConsultViewController.m
//  Hero
//
//  Created by lanou3g on 16/5/4.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "ConsultViewController.h"
#import "NewView.h"
#import "ActivetableView.h"
#import "VidioView.h"
#import "WebViewController.h"
#import "ConsultModel.h"
#import "SearchTableViewController.h"
#define kWidth  [UIScreen mainScreen].bounds.size.width
@interface ConsultViewController ()<TableSelectCellDeledate,UIScrollViewDelegate,ViewSelectCellDelegate,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (nonatomic, strong) NewView *oneView;
@property (nonatomic, strong) ActivetableView *twoView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) VidioView *threeView;
@property (nonatomic, strong) NSString *searchText;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ConsultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor cyanColor];
    self.dataArray = [NSMutableArray new];
    self.myScrollView.contentSize = CGSizeMake(kWidth * 3, 0);
    self.myScrollView.pagingEnabled = YES;
    self.oneView = [[NewView alloc]initWithFrame:CGRectMake(0, 0, kWidth, self.myScrollView.frame.size.height)];
    self.oneView.myDelegate = self;
    [self.myScrollView addSubview:self.oneView];
    
    self.twoView = [[ActivetableView alloc]initWithFrame:CGRectMake(kWidth , 0, kWidth, self.myScrollView.frame.size.height)];
    self.twoView.myDelegate = self;
    self.twoView.dataArray1 = self.twoView.dataArray;
    [self.myScrollView addSubview:self.twoView];
    self.threeView = [[VidioView alloc]initWithFrame:CGRectMake(kWidth * 2, 0, kWidth, self.myScrollView.frame.size.height)];
    [self.myScrollView addSubview:self.threeView];
    [self.myScrollView bringSubviewToFront:self.threeView];
    self.myScrollView.delegate  = self;
    self.myScrollView.bounces = NO;
    UIButton *button = (UIButton *)[self.view viewWithTag:1000];
    button.selected = YES;
    self.searchBar.delegate = self;
    
}
//搜索框
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.searchText = searchText;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://qt.qq.com/php_cgi/lol_mobile/iso/php/search_articles.php?keyword=%@&num=10&page=0&plat=android&version=9695",self.searchText]];
    [YQNetWorkRequestManager requestURL:url method:YQ_GET params:nil result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            [self.dataArray removeAllObjects];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//             NSLog(@"%@",dict);
            if ([dict[@"is_end"] isEqual:@1]) {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"并没有搜索结果" preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
                dispatch_async(dispatch_get_main_queue(), ^{
                     [self presentViewController:alert animated:YES completion:nil];
                });
               
            }else{
            for (NSDictionary *dic  in dict[@"list"]) {
                ConsultModel *model = [ConsultModel new];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                SearchTableViewController *VC = [SearchTableViewController new];
                [VC.dataArray removeAllObjects];
                VC.dataArray = self.dataArray;
                [self.navigationController pushViewController:VC animated:YES];
            });
            }
        }
    }];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIButton *button = (UIButton *)[self.view viewWithTag:1000];
    UIButton *button2 = (UIButton *)[self.view viewWithTag:1001];
    UIButton *button3 = (UIButton *)[self.view viewWithTag:1002];
    button.selected = NO;
    button2.selected = NO;
    button3.selected = NO;
    if (scrollView.contentOffset.x == 0) {
        
        button.selected = YES;
    }else if (scrollView.contentOffset.x >= kWidth && scrollView.contentOffset.x < kWidth * 2) {
        button2.selected = YES;
    }
    else {
         button3.selected = YES;
    }
       
    
}
//代理传值
- (void)didSelectCellWithModel:(ConsultModel *)model
{
    WebViewController *webViewVC = [WebViewController new];
    
    webViewVC.model = model;
    [self.navigationController pushViewController:webViewVC animated:YES];
}
//代理传值
- (void)didSelectCellDeledateWithModel:(ConsultModel *)model
{
    WebViewController *webViewVC = [WebViewController new];
    webViewVC.model = model;
    [self.navigationController pushViewController:webViewVC animated:YES];
}
- (IBAction)didClick1Action:(UIButton *)sender {
    sender.selected = YES;
    [self.myScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    UIButton *button2 = (UIButton *)[self.view viewWithTag:1001];
    UIButton *button3 = (UIButton *)[self.view viewWithTag:1002];
    button2.selected = NO;
    button3.selected = NO;
}
- (IBAction)didClick2Action:(UIButton *)sender {
    sender.selected = YES;
    [self.myScrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
 UIButton *button = (UIButton *)[self.view viewWithTag:1000];
    button.selected = NO;
    UIButton *button3 = (UIButton *)[self.view viewWithTag:1002];
    button3.selected = NO;
    
}
- (IBAction)didClick3Action:(UIButton *)sender {
    sender.selected = YES;
    [self.myScrollView setContentOffset:CGPointMake(kWidth * 2, 0) animated:YES];
  UIButton *button = (UIButton *)[self.view viewWithTag:1000];
    button.selected = NO;
    UIButton *button2 = (UIButton *)[self.view viewWithTag:1001];
    button2.selected = NO;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
